package com.crazymakercircle.netty.codec;

import com.crazymakercircle.netty.decoder.Byte2IntegerDecoder;
import com.crazymakercircle.netty.encoder.Integer2ByteEncoder;
import io.netty.channel.CombinedChannelDuplexHandler;

/**
 * create by 尼恩 @ 疯狂创客圈
 **/
public class IntegerDuplexHandler extends CombinedChannelDuplexHandler<
        Byte2IntegerDecoder,
        Integer2ByteEncoder> {
    public IntegerDuplexHandler() {
        super(new Byte2IntegerDecoder(), new Integer2ByteEncoder());
    }

    // 入站
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        // 处理入站事件
        System.out.println("Received message: " + msg);
        // 可以将消息传递给下一个ChannelHandler
        ctx.fireChannelRead(msg);
    }

    // 出站
    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        // 处理出站事件
        System.out.println("Sending message: " + msg);
        // 实际上将消息写出到网络
        ctx.write(msg, promise);
    }
}
